<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once dirname(__FILE__)."/Config.class.php";
require_once dirname(__FILE__)."/Tooltip.class.php";
require_once dirname(__FILE__)."/ObjectContainer.class.php";
require_once dirname(__FILE__)."/Localization.class.php";
require_once dirname(__FILE__)."/LocaleContainer.class.php";

class CraftDisplayer
{	
	static private $instance;
	
	private $reagent_max_characters_default = 13;
	private $path_to_question_mark = "Interface/Icons/INV_Misc_QuestionMark";
	
	private $object_container;
	private $config;
	private $tooltip;
	private $locale_container;
	private $localization;
	
	private function __construct()
	{
		$this->object_container = ObjectContainer::GetInstance();
		$this->config = Config::GetInstance();
		$this->tooltip = Tooltip::GetInstance();
		$this->locale_container = LocaleContainer::GetInstance();
		$this->localization = Localization::GetInstance();
	}
	
	static function GetInstance()
	{
		if ( empty(self::$instance) )
		{
			self::$instance = new CraftDisplayer();
		}
		
		return self::$instance;
	}
	
	function Name($craft)
	{
		$string = null;
		$object = null;
		$link = null;
		
		if ($craft->IsItem())
		{
			$object = $this->object_container->GetItem( $craft->GetItemId() );
			$link = str_replace("%s", $object->GetItemId(), $this->config->GetItemDatabase());
		}
		else
		{
			$object = $this->object_container->GetSpell( $craft->GetSpellId() );
			$link = str_replace("%s", $object->GetSpellId(), $this->config->GetSpellDatabase());
		}
		
		$string .= "<img src='' title='".( file_exists(dirname(__FILE__)."/../".$object->GetTexture().".png") ? $object->GetTexture() : $this->path_to_question_mark ).".png' alt='' style='float: left; padding-right: 5px; vertical-align: middle;' />";
		$string .= " ";
		
		if ( $craft->HasNameThisLocale() )
		{
			$string .= "<a href='".$link."'>";
			$string .= "<span style='color: #".$object->GetColor().";' ";
			$string .= "onmouseover=\"return overlib('".$this->tooltip->Flatten($this->tooltip->MakeWoWTable($object->GetTooltipThisLocale()))."', FULLHTML);\" ";
			$string .= "onmouseout=\"return nd();\" ";
			$string .= ">";
			$string .= $craft->GetNameThisLocale();
			$string .= "</span>";
			$string .= "</a>";
		}
		
		foreach ( $craft->GetNamesOtherLocales() as $locale => $v )
		{
			$string .= "<br />";
			$string .= "<a href='".$link."'>";
			$string .= "<em ";
			$string .= "onmouseover=\"return overlib('".$this->tooltip->Flatten($this->tooltip->MakeWoWTable($object->GetTooltipLocale($locale)))."', FULLHTML);\" ";
			$string .= "onmouseout=\"return nd();\" ";
			$string .= "style='color: #".$object->GetColor().";'>".$v."</em>";
			$string .= "</a>";
		}
		
		return $string;
	}
	
	function Reagents($craft)
	{
		$string = null;
		
		$string .= "<table cellspacing='0'>";
		
		foreach ( $craft->GetReagents() as $id => $quantity )
		{
			$object = $this->object_container->GetItem($id);
			
			$string .= "<tr>";
			
			$string .= "<td style='vertical-align: top;'>";
			$string .= $quantity."x ";
			$string .= "</td>";
			
			$string .= "<td style='vertical-align: top;'>";
			$string .= "<img src='' title='".( file_exists(dirname(__FILE__)."/../".$object->GetTexture().".png") ? $object->GetTexture() : $this->path_to_question_mark ).".png' alt='' style='height: 15px; vertical-align: middle;' />";
			$string .= "</td>";
			
			$string .= "<td>";
			
			if ( $craft->HasReagentsThisLocale() )
			{
			
				$string .= "<a href='".str_replace("%s", $object->GetItemId(), $this->config->GetItemDatabase())."'>";
				$string .= "<span style='color: #".$object->GetColor().";' ";
				$string .= "onmouseover=\"return overlib('".$this->tooltip->Flatten($this->tooltip->MakeWoWTable($object->GetTooltipThisLocale()))."', FULLHTML);\" ";
				$string .= "onmouseout=\"return nd();\"";
				$string .= ">";
				$string .= $object->GetNameThisLocale();
				$string .= "</span>";
				$string .= "</a> ";
			}
			
			$has_reagents_other_locales = false;
			
			foreach ( $craft->GetReagentsOtherLocales() as $locale )
			{
				if ($has_reagents_other_locales == false)
				{
					$has_reagents_other_locales = true;
					$string .= "<em>(";
				}
				
				$string .= "<a href='".str_replace("%s", $object->GetItemId(), $this->config->GetItemDatabase())."'>";
				$string .= "<span style='color: #".$object->GetColor()."; font-style: italic;' ";
				
				$string .= "onmouseover=\"return overlib('".$this->tooltip->Flatten($this->tooltip->MakeWoWTable($object->GetTooltipLocale($locale)))."', FULLHTML);\" ";
				$string .= "onmouseout=\"return nd();\"";
				
				$string .= ">";
				$string .= $object->GetNameLocale($locale);
				$string .= "</span>";
				$string .= "</a>";
				
				$string .= ",";
			}
			
			if ($has_reagents_other_locales == true)
			{
				$string = substr($string, 0, strlen($string)-1);
				$string .= ")</em>";
			}
			
			$string .= "</td>";
			$string .= "</tr>";
		}
		
		$string .= "</table>";
		
		return $string;
	}
	
	function Characters($craft)
	{
		$string = null;
		
		if ( count($craft->GetCharacters()) > $this->reagent_max_characters_default )
		{
			$string .= implode( ", ", array_slice($craft->GetCharacters(), 0, $this->reagent_max_characters_default) );
			$string .= ", ";
			
			$string .= "<span id='".$craft->GetUniqueId()."_more' onclick=\"more_less('".$craft->GetUniqueId()."');\">";
			$string .= "(";
			$string .= "<em><a href='javascript:;'>".$this->localization->Get('more...')."</a></em>";
			$string .= ")";
			$string .= "</span>";
			
			$string .= "<span id='".$craft->GetUniqueId()."_chars' style='display: none;'>";
			$string .= implode( ", ", array_slice($craft->GetCharacters(), $this->reagent_max_characters_default, count($craft->GetCharacters()) - $this->reagent_max_characters_default ) );		
			$string .= ". ";
			$string .= "</span>";
			
			$string .= "<span id='".$craft->GetUniqueId()."_less' onclick=\"more_less('".$craft->GetUniqueId()."');\" style='display: none;'>";
			$string .= "(";
			$string .= "<em><a href='javascript:;' style='color: rgb(190, 0, 0);'>".$this->localization->Get('less...')."</a></em>";
			$string .= ")";
			$string .= "</span>";
		}
		else
		{
			$string .= implode(", ", $craft->GetCharacters()).".";
		}
		
		return $string;
	}
}

?>